﻿Public Class FrmHangHoa
    Dim provider As New DataProvider
    Dim flag As String = 0
    Dim sql As String

    
    Private Sub LoadHangHoa()
        sql = "SELECT hh.MaHH, hh.TenHH, hh.DonViTinh, nhh.TenNhomHH FROM HangHoa hh, NhomHangHoa nhh WHERE hh.MaNhomHH = nhh.MaNhomHH"
        dtghanghoa.DataSource = provider.ExecuteQuery(sql)

        LoadNhomHH()
        Enable(False)
        btnluu.Enabled = False
        btnhuy.Enabled = False
        btnthem.Enabled = True
        btnsua.Enabled = True

    End Sub
    Private Sub LoadNhomHH()
        sql = "select * from nhomhanghoa"
        cbxnhomhh.DataSource = provider.ExecuteQuery(sql)
        cbxnhomhh.DisplayMember = "TenNhomHH"
        cbxnhomhh.ValueMember = "MaNhomHH"

    End Sub
    Private Sub Enable(ByVal ena As Boolean)
        txtdonvitinh.Enabled = ena
        txttenhh.Enabled = ena
        txtmahh.Enabled = ena
        txtdonvitinh.Enabled = ena
        cbxnhomhh.Enabled = ena
    End Sub

    Private Sub FrmHangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadHangHoa()
    End Sub

    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        flag = 1
        Enable(True)
        btnluu.Enabled = True
        btnhuy.Enabled = True
        txtdonvitinh.Text = ""
        txtmahh.Text = ""
        txttenhh.Text = ""
        btnsua.Enabled = False


    End Sub

    Private Sub dtghanghoa_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtghanghoa.CellClick
        Try
            Dim row As Integer
            Dim dt As New DataTable
            row = dtghanghoa.CurrentCell.RowIndex
            txtmahh.Text = dtghanghoa.Rows(row).Cells(0).Value
            txttenhh.Text = dtghanghoa.Rows(row).Cells(1).Value
            txtdonvitinh.Text = dtghanghoa.Rows(row).Cells(2).Value
            cbxnhomhh.Text = dtghanghoa.Rows(row).Cells(3).Value

        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click
        
        If txtmahh.Text = "" Then
            MessageBox.Show("Chọn 1 hàng hóa để sửa")
        Else
            flag = 2
            txtmahh.Enabled = False
            btnthem.Enabled = False
            btnluu.Enabled = True
            btnhuy.Enabled = True
        End If
    End Sub

    Private Sub btnhuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhuy.Click
        LoadHangHoa()
    End Sub

    Private Sub btnluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnluu.Click

        If flag = 1 Then
            Dim mahh As String
            Dim tenhh As String
            Dim dvt As String
            Dim manhomhh As String
            mahh = txtmahh.Text
            tenhh = txttenhh.Text
            dvt = txtdonvitinh.Text
            manhomhh = cbxnhomhh.SelectedValue

            sql = "select * from HangHoa where mahh='" + mahh + "'"
            Dim dt As New DataTable
            dt = provider.ExecuteQuery(sql)

            If dt.Rows.Count > 0 Then
                sql = "INSERT INTO HangHoa(	MaHH,	TenHH,	DonViTinh,	MaNhomHH)VALUES('" + mahh + "',N'" + tenhh + "',N'" + dvt + "','" + manhomhh + "')"
                provider.ExecuteNonQuery(sql)
                MessageBox.Show("Thêm Hàng Hóa thành công")

            Else
                MessageBox.Show("Mã hàng hóa này đã tồn tại")
            End If

        ElseIf flag = 2 Then
            Dim mahh As String
            Dim tenhh As String
            Dim dvt As String
            Dim manhomhh As String
            mahh = txtmahh.Text
            tenhh = txttenhh.Text
            dvt = txtdonvitinh.Text
            manhomhh = cbxnhomhh.SelectedValue

            sql = "UPDATE HangHoa SET TenHH = N'" + tenhh + "',	DonViTinh = N'" + dvt + "',	MaNhomHH = '" + manhomhh + "' where MaHH='" + mahh + "'"
            provider.ExecuteNonQuery(sql)
            MessageBox.Show("Cập nhật thành công")
        End If

    End Sub
End Class